package com.wut.easypoi.controller;

import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.wut.core.common.vo.BaseResponse;
import com.wut.core.controller.BaseController;
import com.wut.easypoi.common.excel.MemberExcel;
import com.wut.easypoi.common.excel.OrderExcel;
import com.wut.easypoi.service.EasyPoiService;
import com.wut.easypoi.utils.ExcelUtil;
import com.wut.utils.json.LocalJsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequiredArgsConstructor
@RequestMapping("/easypoi")
@Api(tags = "EasyPoi模块")
public class EasyPoiController extends BaseController {

    private final EasyPoiService easyPoiService;

    /**
     * XSSF：XLSX格式图片导出有问题
     */
    @GetMapping("/member/export")
    @ApiOperation("导出会员列表Excel")
    public void exportMember() {
        List<MemberExcel> memberList = LocalJsonUtil.getListFromJson("json/members.json", MemberExcel.class);
        ExcelUtil.exportExcelNoTitle(memberList, MemberExcel.class, "会员信息表", "会员信息", ExcelType.HSSF, response);
    }

    @PostMapping("/member/import")
    @ApiOperation("导入会员列表")
    public BaseResponse<Boolean> importMember(@RequestParam("file") MultipartFile file) {
        List<MemberExcel> memberList = ExcelUtil.importExcel(file, MemberExcel.class);
        return BaseResponse.SUCCESS("导入成功", true);
    }

    @GetMapping("/order/export")
    @ApiOperation("导出订单列表Excel")
    public void exportOrder() {
        List<OrderExcel> orderList = easyPoiService.getOrderList();
        ExcelUtil.exportExcel(orderList, OrderExcel.class, "订单信息表", "订单信息", "订单信息", ExcelType.HSSF, response);
    }
}
